import { serve } from '@hono/node-server'
import { PrismaClient } from '@prisma/client'
import { Hono } from 'hono'
import { sentry } from '@hono/sentry'


const app = new Hono()
app.use('*', sentry({ dsn: 'http://8481c144cad786d25d128f8ba1b5563d@172.18.172.152:9000/6' }))
const prisma = new PrismaClient()

async function getLink(docName:string) {
  if(docName){

    return await prisma.links.findUnique({
      where: {
        docName
      }
    })
  }
  return null
}

app.get('/:docName',  async (c) => {
  const docName = c.req.param('docName')
  
  const link = await getLink(docName)
  console.log(link)
  if (link){
    return c.redirect(encodeURI(link.original), 302)
  }else{
    return c.status(404)
  }
})

const port = 3006
console.log(`Server is running on port ${port}`)

serve({
  fetch: app.fetch,
  port
})
